package com.haoqizhe.kernel.security.helper;

import cn.hutool.core.util.ObjectUtil;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/**
 * 密码加密帮助类
 * @author haoqizhe.li
 * @date 2018/7/10
 **/
public class PasswordHelper {

    /**
     * 将密码使用盐值 md5加密两次
     * @param password 密码明文
     * @param salt 盐值
     * @return
     */
    public static String setPasswd(String password,String salt){
        ByteSource credentialsSalt = ByteSource.Util.bytes(salt);
        Object obj = new SimpleHash("md5", password, credentialsSalt, 2);
        return ObjectUtil.isNotNull(obj) ? obj.toString() : "";
    }

    /**
     *  将密码使用BCryptPasswordEncoder加密，BCryptPasswordEncoder是随机的salt 更加安全
     * @param password  密码明文
     * @return
     */
    public static String setPasswdByBCryptPasswordEncoder(String password){
        return new BCryptPasswordEncoder().encode(password);
    }
}
